package com.cm367.teamc.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

	public static String getStringDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}

	public static Date getParseDate(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static int compareDate(String currentDate, String targetDate) {
		Date c = getParseDate(currentDate);
		Date t = getParseDate(targetDate);
		return c.compareTo(t);
	}

	public static int countDays(int year, String dateFrom, String dateTo,
			List<String> holidays) {
		int days = 0;
		Calendar c = Calendar.getInstance();
		Date date = getParseDate(dateFrom);
		Date date2 = getParseDate(dateTo);
		days = (int) ((date2.getTime() - date.getTime()) / 1000 / 24 / 3600);
		++days;
		c.setTime(date);
		int day = 0;
		for (int i = 0; i < days; i++) {
			if (c.get(Calendar.YEAR) == year) {
				int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
				if (dayOfWeek != 1 && dayOfWeek != 7) {
					if (!holidays.contains(getStringDate(c.getTime()))) {
						day++;
					}
				}
			}
			c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) + 1);
		}
		return day;
	}

	public static int getYear(String date) {
		return Integer.parseInt(date.substring(0, 4));
	}

}
